package nuc.edu.employmentservices.controller;

import com.mysql.cj.Session;
import nuc.edu.employmentservices.enity.Classes;
import nuc.edu.employmentservices.enity.Graduates;
import nuc.edu.employmentservices.service.ClassesService;
import nuc.edu.employmentservices.service.GraduatesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

import java.util.List;

@Controller
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @Autowired
    private GraduatesService graduatesService;

    @GetMapping("/classeslist")
    public String classeslist(Model model)
    {
        List<Classes> classesList=classesService.getAllClasses();
        Integer num = classesService.getClassCount();
        model.addAttribute("classeslist",classesList);
        model.addAttribute("classnumm",num);
        System.out.println(classesList);
        return "classeslist";
    }


    @GetMapping("/classesal/{id}")
    public String classesall(@PathVariable("id") Integer id, HttpSession session){
        List<Graduates> graduatesListbyclassid = graduatesService.getGraduatesByClass(id);
        session.setAttribute("graduatesListbyclassid",graduatesListbyclassid);


        session.setAttribute("classNum",classesService.getClassesById(id).getClassNum());

        return "redirect:/toclassesall";
    }

    @RequestMapping("toclassesall")
    public String toclassesall(Model model,HttpSession session){
        List<Graduates> graduatesListbyclassid = (List<Graduates>)session.getAttribute("graduatesListbyclassid");
        Integer classnum = (Integer) session.getAttribute("classNum");
        model.addAttribute("graduatesclassid",graduatesListbyclassid);
        model.addAttribute("classnum",classnum);
        return "classesall";
    }

}
